package fileparsing;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import commonfunctions.Matrix;

import filewriting.ProbabilityMatrixWriter;


public class ReadProbabilities 
{
	/**
	 * Reads the weights from a file.
	 * 
	 * @param dirName
	 * @return
	 */
	public static Matrix probabilityMatrix(String dirName)
	{
		try
		{
			File f = new File(dirName + "/" + ProbabilityMatrixWriter.PROB_GRAPH_NAME);
			Scanner s = new Scanner(f);
			String firstRow = s.nextLine();
			String[] firstRowSplit = firstRow.split(" ");
			int size = Integer.parseInt(firstRowSplit[1]);
			Matrix toReturn = new Matrix(size);
			int rowNum = 0;
			for (int k = 0; k < size; k++)
			{
				for (int j = 0; j < size; j++)
				{
					toReturn.set(0.0, k, j);
				}
			}
			while(s.hasNext())
			{
				Scanner sLine = new Scanner(s.nextLine());
				while (sLine.hasNext())
				{
					int colNum = sLine.nextInt();
					toReturn.set(sLine.nextDouble(), rowNum, colNum);
				}
				rowNum++;
			}
			return toReturn;
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Could not find file.");
			return new Matrix(0);
		}
	}
}
